package com.mapler.engine.linkbucks;

import com.mapler.tbot.service.*;
import com.mapler.ngfx.service.*;
import com.mapler.model.SModel;
import com.mapler.model.UAModel;
import com.mapler.service.INotifier;
import com.mapler.service.MailRunner;
import com.mapler.utility.Util;
import java.util.List;
import org.apache.log4j.Logger;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

/**
 *
 * @author none
 */
public class LinkBucksClick implements Runnable {

    private SModel sModel;
    private static Logger log = Logger.getLogger(NGFXSignupClick.class);
    private INotifier iNotifier;
    private UAModel uAModel = new UAModel();

    public LinkBucksClick(INotifier iNotifier, SModel sModel) {
        this.sModel = sModel;
        this.iNotifier = iNotifier;
    }

    public LinkBucksClick(SModel iModel) {
        this.sModel = iModel;
    }

    @Override
    public void run() {
        try {
            this.doPost();
        } catch (Throwable ex) {
            iNotifier.notify("LinkBucks:Run:: stopped for error: " + ex.getMessage());
            log.error("LinkBucks:Run:: stopped causes " + ex);
        }
    }

    private void doPost() {
        WebDriver driver = null;
        try {
            driver = Util.createDriver(sModel, uAModel);
            if (driver == null) {
                iNotifier.notify("LinkBucks:: unable to init driver. driver is null");
                log.debug("LinkBucks:: driver is null. Unable to init driver");
                throw new Exception("LinkBucks:: driver is null. Unable to init driver");
            }
            driver.get(sModel.getLink());
            WebElement divEle = driver.findElement(By.id("eow-description"));
            List<WebElement> elements = divEle.findElements(By.tagName("a"));
            
            for (WebElement e : elements){
                System.out.println(e.getText());
                driver.get(e.getText().trim());
                Util.wait(1);
                WebElement clickAdd = driver.findElement(By.id("eow-description"));
                clickAdd.click();
                System.out.println("he he he .....");
                break;
            }
                    
        } catch (Throwable ex) {
            //iNotifier.notify("Tbot:: Stopped for error: " + ex.getMessage());
            log.error("Tbot:: Stopped causes..." + ex);
            ex.printStackTrace();
        } finally {
            if (driver != null) {
                driver.close();
            }
        }
    }

    public static void main(String s[]) {
        SModel sModel = new SModel();
        sModel.setDriver("FF");
        sModel.setLink("https://www.youtube.com/watch?v=EiifYTAwVT4");
        sModel.setMaxWaitTime("1");
        sModel.setResourcesHide(false);
        LinkBucksClick click = new LinkBucksClick(sModel);

        click.doPost();
        //click.doIT();

    }

    private void doIT() {
        SModel sModel = new SModel();
        sModel.setDriver("FF");
        sModel.setLink("http://nigeria-forex.com/forum.php?referrerid=27246");
        sModel.setUseFWlink("0");
        sModel.setUsername("mithun");
        sModel.setPassword("mithun");
        NGFXSignupClick click = new NGFXSignupClick(sModel);
        UAModel model = new UAModel();
        model.setEmail("skyroproject8@gmail.com");
        model.setEmailPassword("mn142536");
        model.setUsername("prantoor");
        model.setPassword("pass123");
        model.setId("22");
        MailRunner mailRunner = new MailRunner(iNotifier, sModel, model);
        mailRunner.checkNGFXEmail(null);
    }
}